<?php
class Application_Form_Decorator_View_Common extends Zend_Form_Decorator_Abstract
{
	/**
	 * 
	 * @var string
	 */
	protected $_multiValueSeparator = '<br />';
	/**
	 * 
	 * @param string $separator
	 * @return Application_Form_Decorator_View_Common
	 */
	public function setMultiValueSeparator ($separator) 
	{
		$this->_multiValueSeparator = $separator;
		return $this;	
	}
	/**
	 * 
	 * @return string
	 */
	public function getMultiValueSeparator ()
	{
		return $this->_multiValueSeparator;
	}
	/**
	 * 
	 * @param string $content
	 */
	public function render ($content) 
	{
		// get element
        $element 		= $this->getElement();
        
		// get element label and assign the default display value.
        $label 			= $element->getLabel();
        $displayValue	= '';

        // some elements need to be ignored during the rendering in view mode, ignore these elements.
        if ($element instanceof Zend_Form_Element_Hidden || $element instanceof Zend_Form_Element_Password) {
        	return $displayValue;
        }
               
        // if content is passed in, assume a dedicated view helper has generated it and 'trust' it.
        if (strlen($content) > 0) {
        	$value = $element->getUnfilteredValue();
        } else {
        	$value = $element->getValue();
        }
        
		// elements that can contain multiple options need to be rendered diffrently, we render all its values separated with the multiSeparatorValue.
        if ($element instanceof Zend_Form_Element_Multi) {
        	
        	$options = $element->getMultiOptions();
        	
        	if (!is_array($value)) {
        		$value = array($value);
        	}
        	
        	// separator is empty first (handy in case separator is a , character).
        	$separator = ''; 
        	foreach ($value as $singleValue) {
        		if (array_key_exists($singleValue, $options)) {
        			$displayValue 	.= $separator . $options[$singleValue];
        			$separator 		= $this->getMultiValueSeparator();
        		}
        	}
        } else {
        	$displayValue = $value;
        }

        return sprintf('<dt>%s</dt><dd>%s<dd>', 
        	$label,
        	$displayValue
        ); 
	}
}